#!/usr/bin/env python
#coding:utf-8

import sys, os, time, wx
import Globals
import Debug
import common
import Database as db
import RTControl as RTC
from RTControl import Outputor
import threading

reload(sys)
sys.setdefaultencoding('utf-8') 

DEBUG = True

workpath = os.path.dirname(os.path.realpath(sys.argv[0]))
confpath = os.path.join(workpath, 'conf')
datapath = os.path.join(workpath, 'data')
sys.path.insert(0, workpath)
Globals.workpath = workpath
Globals.confpath = confpath
Globals.datapath = datapath

ini = common.get_config_file_obj()
debugflag = ini.default.get('debug', False)

Debug.debug = Debug.Debug(os.path.join(workpath, 'debug.txt'), debugflag)
Debug.error = Debug.Debug(os.path.join(workpath, 'error.txt'), True)

from MainFrame import MainFrame
import ControlDomain
import Domain
import ViewControler
import Preference
import Model

class wxApp(wx.App):
    
    def OnInit(self):
        wx.Log.SetLogLevel(wx.LOG_Error)
        self.frame = MainFrame()
        self.frame.Show()
        self.SetTopWindow(self.frame)
        
        Globals.mainframe = self.frame
        
        Debug.debug.info(u"系统初始化完成...")
        return True
    
    def OnExit(self):
        app.OnExit()
        Debug.debug.info(u"系统退出。")
    
class App():
    
    def __init__(self):
        
        import __builtin__
        __builtin__.__dict__['app'] = self
        
        if db.Connect():
            db.Check()
        
        Globals.app = self
        self.pref = Preference.Preference()
        self.LoadPrefPage()
        self.LoadPref()
        Globals.pref = self.pref
        
        RTC.RTControlInit(20)
        
        from RTControl import VariablePool as VP
        self.vp = VP.varpool
        
        self.CreateDomainObject()
        self.CreateControllerObject()
        
        self.domain.SetPartType(self.pref.part_type)
        
        ########################################################
        
        self.wxapp = wxApp(0)
        self.wxapp.MainLoop()
        
    def AddTask(self, callobj, *args, **kwargs):
        RTC.AddTask(callobj, *args, **kwargs)
        
    def AddOPCTask(self, callobj, *args, **kwargs):
        self.opcthreadpool.AddTask(callobj, *args, **kwargs)
        
    def OnExit(self):
        self.domain.EndWork()
        self.condomain.EndWork()
        RTC.RTControlExit()
        
    def CreateDomainObject(self):
        self.condomain = ControlDomain.ControlDomain()
        self.domain = Domain.PWDomain()
        
    def CreateControllerObject(self):
        self.viewcon = ViewControler.ViewController(self.domain, self.condomain)
        self.domain.SetControl(self.viewcon)
        
    def LoadPrefPage(self):
        self.pref.pages_order.update({
                                      u'物理参数':100,
                                      u'物理参数/左侧坐标系':100,
                                      u'物理参数/右侧坐标系':110,
                                      u'物理参数/零平面间距':120,
                                      u'零平面校准':110,
                                      u'零平面校准/左侧':100,
                                      u'零平面校准/左侧/标准值':100,
                                      u'零平面校准/右侧':110,
                                      u'零平面校准/右侧/标准值':100,
                                      u'传感器补偿':120,
                                      u'传感器补偿/左侧':100,
                                      u'传感器补偿/右侧':100,
                                      u'默认加工件':130,
                                      u'自动启动模式':140,
                                      u'高级':150})
        
    def LoadPref(self):
        self.pref.layout = ''
        self.pref.left_y1 = 10.0
        self.pref.left_x2 = 10.0
        self.pref.left_x3 = 10.0
        self.pref.left_y3 = 10.0
        self.pref.right_y4 = 10.0
        self.pref.right_x5 = 10.0
        self.pref.right_x6 = 10.0
        self.pref.right_y6 = 10.0
        self.pref.zero_dis = 10.0
        
        self.pref.preflist.extend([
            (u'物理参数/左侧坐标系', 140, 'float', 'left_y1', u'左侧水平传感器间距Y1', 'mm'),
            (u'物理参数/左侧坐标系', 140, 'float', 'left_x2', u'左侧垂直传感器间距X2', 'mm'),
            (u'物理参数/左侧坐标系', 140, 'float', 'left_x3', u'左侧水平传感器间距X3', 'mm'),
            (u'物理参数/左侧坐标系', 140, 'float', 'left_y3', u'左侧垂直传感器间距Y3', 'mm'),
            (u'物理参数/右侧坐标系', 140, 'float', 'right_y4', u'右侧水平传感器间距Y4', 'mm'),
            (u'物理参数/右侧坐标系', 140, 'float', 'right_x5', u'右侧垂直传感器间距X5', 'mm'),
            (u'物理参数/右侧坐标系', 140, 'float', 'right_x6', u'右侧水平传感器间距X6', 'mm'),
            (u'物理参数/右侧坐标系', 140, 'float', 'right_y6', u'右侧垂直传感器间距Y6', 'mm'),
            (u'物理参数/零平面间距', 140, 'float', 'zero_dis', u'零平面间距', 'mm')
            ])
        
        self.pref.left_zero_pv1 = 0.0
        self.pref.left_zero_pv2 = 0.0
        self.pref.left_zero_pv3 = 0.0
        self.pref.right_zero_pv4 = 0.0
        self.pref.right_zero_pv5 = 0.0
        self.pref.right_zero_pv6 = 0.0
        
        self.pref.left_zero_pv1_std = 0.0
        self.pref.left_zero_pv2_std = 0.0
        self.pref.left_zero_pv3_std = 0.0
        self.pref.right_zero_pv4_std = 0.0
        self.pref.right_zero_pv5_std = 0.0
        self.pref.right_zero_pv6_std = 0.0
        
        self.pref.left_zero_pv1_offset = 0.0
        self.pref.left_zero_pv2_offset = 0.0
        self.pref.left_zero_pv3_offset = 0.0
        self.pref.right_zero_pv4_offset = 0.0
        self.pref.right_zero_pv5_offset = 0.0
        self.pref.right_zero_pv6_offset = 0.0
        
        self.pref.preflist.extend([
            (u'零平面校准/左侧', 140, 'float', 'left_zero_pv1', u'左侧零平面校准值PV1', 'mm'),
            (u'零平面校准/左侧', 140, 'float', 'left_zero_pv2', u'左侧零平面校准值PV2', 'mm'),
            (u'零平面校准/左侧', 140, 'float', 'left_zero_pv3', u'左侧零平面校准值PV3', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv1_std', u'左侧零平面PV1标准值', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv1_offset', u'左侧零平面PV1公差', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv2_std', u'左侧零平面PV2标准值', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv2_offset', u'左侧零平面PV2公差', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv3_std', u'左侧零平面PV3标准值', 'mm'),
            (u'零平面校准/左侧/标准值', 140, 'float', 'left_zero_pv3_offset', u'左侧零平面PV3公差', 'mm'),
            (u'零平面校准/右侧', 140, 'float', 'right_zero_pv4', u'右侧零平面校准值PV1', 'mm'),
            (u'零平面校准/右侧', 140, 'float', 'right_zero_pv5', u'右侧零平面校准值PV2', 'mm'),
            (u'零平面校准/右侧', 140, 'float', 'right_zero_pv6', u'右侧零平面校准值PV3', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv4_std', u'右侧零平面PV4标准值', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv4_offset', u'右侧零平面PV1公差', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv5_std', u'右侧零平面PV5标准值', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv5_offset', u'右侧零平面PV5公差', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv6_std', u'右侧零平面PV6标准值', 'mm'),
            (u'零平面校准/右侧/标准值', 140, 'float', 'right_zero_pv6_offset', u'右侧零平面PV6公差', 'mm'),
            ])
        
        self.pref.left_pv1_offset = 0.0
        self.pref.left_pv2_offset = 0.0
        self.pref.left_pv3_offset = 0.0
        self.pref.right_pv4_offset = 0.0
        self.pref.right_pv5_offset = 0.0
        self.pref.right_pv6_offset = 0.0
        
        self.pref.preflist.extend([
            (u'传感器补偿/左侧', 140, 'float', 'left_pv1_offset', u'左侧PV1补偿', 'mm'),
            (u'传感器补偿/左侧', 140, 'float', 'left_pv2_offset', u'左侧PV2补偿', 'mm'),
            (u'传感器补偿/左侧', 140, 'float', 'left_pv3_offset', u'左侧PV3补偿', 'mm'),
            (u'传感器补偿/右侧', 140, 'float', 'right_pv4_offset', u'右侧PV4补偿', 'mm'),
            (u'传感器补偿/右侧', 140, 'float', 'right_pv5_offset', u'右侧PV5补偿', 'mm'),
            (u'传感器补偿/右侧', 140, 'float', 'right_pv6_offset', u'右侧PV6补偿', 'mm'),
            ])
        
        self.pref.part_type = ''
        names = [''] + Model.MPartTypes.GetNames()
        self.pref.preflist.extend([
            (u'默认加工件', 140, 'choice', 'part_type', u'加工件型号', names),
            ])
        
        self.pref.no_barcode = False
        self.pref.preflist.extend([
            (u'自动启动模式', 140, 'check', 'no_barcode', u'使用无条码模式，该模式下启动按钮有效', None),
            ])
        
        self.pref.re_barcode_type = 'P(\\w+)\\x1d|\\d{2}(\\w{4})H'
        self.pref.preflist.extend([
            (u'高级', 140, 'text', 're_barcode_type', u'总成型号匹配正则表达式', None),
            ])
        
        self.pref.max_diff_val = 0.03
        self.pref.enable_type_sensor = True
        self.pref.preflist.extend([
            (u'高级', 140, 'float', 'max_diff_val', u'校准最大偏差', 'mm'),
            (u'高级', 141, 'check', 'enable_type_sensor', u'是否开启高、中、低配检测', None)
            ])
        
        try:
            self.pref.load()
        except:
            Debug.error.traceback()
        
        if not self.pref.part_type in names:
            self.pref.part_type = ''
            self.pref.save()
        
        
if __name__ == '__main__':
    App()